/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.fw.types;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.bind.annotation.XmlRootElement;

import net.interknit.knitter.fw.CustomTypeFactory;

@XmlRootElement(name = "net.interknit.knitter.fw.types.RawFile")
public class RawFile extends java.io.File
{
	/**
	 * The underlying state maintained by this adapter
	 */
	private File state;

	/**
	 * Private method to provide the "default" state for this adapter
	 */
	private File defaultState()
	{
		return state != null ? state : (state = CustomTypeFactory.getInstance().instantiate(File.class));
	}

	/**
	 * Private method to provide the "default" value for this adapter
	 */
	private File defaultValue(File v)
	{
		return v != null ? v : CustomTypeFactory.getInstance().instantiate(File.class);
	}

	/**
	 * Default no-arg constructor
	 */
	public RawFile()
	{
		this(CustomTypeFactory.getInstance().instantiate(File.class));
	}

	/**
	 * Construct this instance from the Object parameter.
	 *
	 * @throws ClassCastException if the parameter obj is not of type File
	 */
	public RawFile(java.lang.Object obj)
	{
		this((File) obj);
	}

	/**
	 * Constructor to accept the user values and convert it to the underlying state.
	 * In other words, this constructor provides a way for "wrapping" the user value into this adapter.
	 * This constructor is also known as 'value constructor'
	 */
	public RawFile(File value)
	{
		super(
				(value != null ? value : CustomTypeFactory.getInstance().instantiate(File.class))
				.getAbsolutePath());

		this.state = defaultValue(value);
	}

	/**
	 * Adapter method to convert the underlying state back to user value.
	 * In other words, this method provides a way for "unwrapping" the user value from this adapter that
	 * was previously wrapped using the value constructor.
	 */
	public <T extends File> T unwrap(Class<T> type)
	{
		return (T) defaultState();
	}

	/**
	 * Return the underlying state of this adapter.
	 *
	 * @return the underlying state of this adapter
	 */
	public byte[] getValue()
	{
		try
		{
			byte[] bytes = new byte[0];

			File f = defaultState();

			if(f.exists() && ! f.isDirectory())
			{
				FileInputStream in = new FileInputStream(defaultState());

				bytes = new byte[in.available()];

				in.read(bytes);
				in.close();
			}

			return bytes;
		}
		catch(java.lang.Exception e)
		{
			throw new java.lang.RuntimeException(e);
		}
	}

	/**
	 * Sets the underlying state of this adapter with the given state parameter.
	 *
	 * @param value the underlying state to set
	 */
	public void setValue(byte[] value)
	{
		try
		{
			this.state = File.createTempFile("rawfile", ".tmp");

			FileOutputStream out = new FileOutputStream(this.state);

			out.write(value);
			out.close();
		}
		catch(java.lang.Exception e)
		{
			throw new java.lang.RuntimeException(e);
		}
	}
}
